package com.qianfeng.xinyihezi.xinyihezi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.adapter.MyListViewAdapter2;
import com.qianfeng.xinyihezi.xinyihezi.config.MyNetConfig;
import com.qianfeng.xinyihezi.xinyihezi.config.MyRetrofit;
import com.qianfeng.xinyihezi.xinyihezi.model.RankBean;
import com.qianfeng.xinyihezi.xinyihezi.model.RankingTitleBean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * 作者：王加威 on 2016/11/15 14:59
 */

public class RankingActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener, RadioGroup.OnCheckedChangeListener, View.OnClickListener {



    private String mRc_id;
    private ImageButton iv1,iv2,iv3,iv4,iv5,iv6;
    private List<RankingTitleBean>mTitleBeen;
    private RadioGroup mGroup;
    private RadioButton[] mButtons;
    private SwipeRefreshLayout mRefreshLayout;
    private ListView mListView;
    private List<RankBean.DataBean>mRankBeen;
    private MyListViewAdapter2 mAdapter;
    private ImageView mBack;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ranking_activity);
        Intent intent=getIntent();
        mRc_id = intent.getStringExtra("rc_id");
        Log.e("mRc_id",mRc_id);
        initView();
        initData();
        setAdapter();
        setListener();
    }

    private void setAdapter() {
        mListView.setAdapter(mAdapter);
    }

    private void setListener() {
        mRefreshLayout.setOnRefreshListener(this);
        mGroup.setOnCheckedChangeListener(this);
        mBack.setOnClickListener(this);
    }



    private void initData() {
        Retrofit retrofit=new Retrofit.Builder().baseUrl(MyNetConfig.BASE_PATH).build();
        MyRetrofit myRetrofit=retrofit.create(MyRetrofit.class);
        myRetrofit.getRankingData().enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    try {
                        RankingTitleBean t=new Gson().fromJson(response.body().string(),RankingTitleBean.class);
                        Glide.with(RankingActivity.this).load(t.data.category_list.get(0).image_url).into(iv1);
                        Glide.with(RankingActivity.this).load(t.data.category_list.get(1).image_url).into(iv2);
                        Glide.with(RankingActivity.this).load(t.data.category_list.get(2).image_url).into(iv3);
                        Glide.with(RankingActivity.this).load(t.data.category_list.get(3).image_url).into(iv4);
                        Glide.with(RankingActivity.this).load(t.data.category_list.get(4).image_url).into(iv5);
                        Glide.with(RankingActivity.this).load(t.data.category_list.get(5).image_url).into(iv6);
                    }  catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    private void initView() {

        mBack= (ImageView) findViewById(R.id.ranking_activity_back);
        mRankBeen=new ArrayList<>();
        mAdapter=new MyListViewAdapter2(this,mRankBeen);
        mListView= (ListView) findViewById(R.id.ranking_activity_listview);
        mRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.ranking_activity_SwipeRefreshLayout);;
        iv1= (ImageButton) findViewById(R.id.ranking_activity_zb);
        iv2= (ImageButton) findViewById(R.id.ranking_activity_lwb);
        iv3= (ImageButton) findViewById(R.id.ranking_activity_ms);
        iv4= (ImageButton) findViewById(R.id.ranking_activity_mz);
        iv5= (ImageButton) findViewById(R.id.ranking_activity_sm);
        iv6= (ImageButton) findViewById(R.id.ranking_activity_jj);
        mTitleBeen=new ArrayList<>();
        mGroup= (RadioGroup) findViewById(R.id.ranking_activity_rg);
        mButtons=new RadioButton[mGroup.getChildCount()];
        for (int i = 0; i < mButtons.length; i++) {
            RadioButton radioButton= (RadioButton) mGroup.getChildAt(i);
            mButtons[i]=radioButton;
        }
        if (mRc_id.equals("14")) {/*家居*//*13家居*/
            Toast.makeText(this, "暂无数据", Toast.LENGTH_SHORT).show();
            mButtons[5].setChecked(true);
            mRankBeen.clear();
        }else if(mRc_id.equals("15")){/*数码*/
            parseJson(mRc_id);
            mButtons[4].setChecked(true);
        }else if(mRc_id.equals("11")){/*美妆*/
            parseJson(mRc_id);
            mButtons[3].setChecked(true);
        }else {
            Toast.makeText(this, "暂无数据", Toast.LENGTH_SHORT).show();
        }
    }

    private void parseJson(String rc_id) {
        Retrofit retrofit=new Retrofit.Builder().baseUrl(MyNetConfig.BASE_PATH).build();
        MyRetrofit myRetrofit=retrofit.create(MyRetrofit.class);
        myRetrofit.getRank(rc_id).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    try {
                        mRankBeen.clear();
                        RankBean bean=new Gson().fromJson(response.body().string(),RankBean.class);
                        mRankBeen.addAll(bean.data);
                        mRefreshLayout.setRefreshing(false);
                        mAdapter.notifyDataSetChanged();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(RankingActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
            }
        });
    }



    @Override
    public void onRefresh() {
        parseJson(mRc_id);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int i = 0; i < mGroup.getChildCount(); i++) {
            if (mButtons[0].getId()==checkedId) {
                mRankBeen.clear();
                mAdapter.notifyDataSetChanged();
                Toast.makeText(this, "暂无数据", Toast.LENGTH_SHORT).show();
            }else if (mButtons[1].getId()==checkedId){
                mRankBeen.clear();
                mAdapter.notifyDataSetChanged();
                Toast.makeText(this, "暂无数据", Toast.LENGTH_SHORT).show();
            }else if (mButtons[2].getId()==checkedId){
                mRankBeen.clear();
                mAdapter.notifyDataSetChanged();
                Toast.makeText(this, "暂无数据", Toast.LENGTH_SHORT).show();
            }else if (mButtons[3].getId()==checkedId){
                    parseJson("11");
            }else if (mButtons[4].getId()==checkedId){
                    parseJson("15");
            }else if (mButtons[5].getId()==checkedId){
                    parseJson("13");
            }
        }
    }

    @Override
    public void onClick(View v) {
        finish();
    }
}
